package br.ita.magiu;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import br.ita.magiu.dummy.DummyStudent;
import br.ita.magiu.html.HtmlComposer;
import br.ita.magiu.metadata.util.BrazilianDateFormatter;
import br.ita.magiu.metadata.util.MetadataUtil;
import br.ita.magiu.metadata.util.TypeFormatter;

@RunWith(JMock.class)
public class MAGIUTest {

	private MAGIU magiu;
	private Mockery context = new JUnit4Mockery() {{
		setImposteriser(ClassImposteriser.INSTANCE);
	}};
	private ApplicationContext applicationContext;
	
	@Before
	public void setUp() {
		applicationContext = ApplicationContext.getInstance();
		applicationContext.setTemplatePath("tests/br/ita/magiu/html/testtemplates");
		magiu = new MAGIU(applicationContext);
	}
	
	@After
	public void tearDown() {
		applicationContext.setTemplatePath(ApplicationContext.TEMPLATES_PATH);
	}
	
	@Test
	public void displayForIntegration() {
		final HtmlComposer composerMock = context.mock(HtmlComposer.class); 
		
		magiu.setHtmlComposer(composerMock);
		
		final DummyStudent dummy = new DummyStudent();
		
		context.checking(new Expectations() {{
			one(composerMock).displayFor(dummy);
		}});
		
		magiu.displayFor(dummy);
	}
	
	@Test
	public void editorForIntegration() {
		final HtmlComposer composerMock = context.mock(HtmlComposer.class); 
		
		magiu.setHtmlComposer(composerMock);
		
		final DummyStudent dummy = new DummyStudent();
		
		context.checking(new Expectations() {{
			one(composerMock).editorFor(dummy);
		}});
		
		magiu.editorFor(dummy);
	}
	
	@Test
	public void editorForWithTemplateNameIntegration() {
		final HtmlComposer composerMock = context.mock(HtmlComposer.class); 
		
		magiu.setHtmlComposer(composerMock);
		
		final DummyStudent dummy = new DummyStudent();
		final String templateName = "DummyStudent";
		context.checking(new Expectations() {{
			one(composerMock).editorFor(templateName, dummy);
		}});
		
		magiu.editorFor(templateName, dummy);
	}
	
	
	@Test
	public void displayForWithTemplateNameIntegration() {
		final HtmlComposer composerMock = context.mock(HtmlComposer.class); 
		
		magiu.setHtmlComposer(composerMock);
		
		final DummyStudent dummy = new DummyStudent();
		final String templateName = "DummyStudent";
		context.checking(new Expectations() {{
			one(composerMock).displayFor(templateName, dummy);
		}});
		
		magiu.displayFor(templateName, dummy);
	}
	
	
	@Test
	public void metadataUtilIntegration() {
		final MetadataUtil utilities = context.mock(MetadataUtil.class);
		
		magiu.setMetadataUtil(utilities);
		final TypeFormatter formatter = new BrazilianDateFormatter();
		
		context.checking(new Expectations() {{
			one(utilities).registerDefaultTemplateName(String.class, "Text");
			one(utilities).registerTypeFormatter(formatter);
		}});
		
		magiu.registerDefaultTemplateName(String.class, "Text");
		magiu.registerTypeFormatter(formatter);
		
	}
}